#!/bin/sh
set -e

if [ "$1" = "" ]; then
  echo "Error: Missing output folder path"
  echo "Usage: buildx11 /path/to/build/directory"
  exit 1
fi

echo "Creating translation binaries..."
if [ ! -d "$1/lang" ]; then
  mkdir $1/lang
fi
for file in *.ts; do
  basename=`basename "$file" .ts`
  lrelease "$file" -qm "$1/lang/$basename.qm"
done
echo "Done."
echo "Copying system translations..."
cp -v -r ./resources/lang/system $1/lang
echo "Done."

echo "Building resource binary..."
rcc -binary resource.qrc -o $1/lmc.rcc
echo "Done."

echo "Copying themes..."
cp -v -r ./resources/themes "$1"
rm -r -v "$1/themes/Classic"
echo "Done."

cp -v ./resources/text/license.txt $1/COPYING

echo "Copying sound files..."
cp -v -r ./resources/sounds $1
echo "Done."

echo "Copying plugins..."
if [ ! -d "$1/imageformats" ]; then
  mkdir $1/imageformats
fi
cp -v $QTDIR/plugins/imageformats/libqgif.so $1/imageformats
cp -v $QTDIR/plugins/imageformats/libqico.so $1/imageformats
cp -v $QTDIR/plugins/imageformats/libqjpeg.so $1/imageformats
# cp -v $QTDIR/plugins/imageformats/libqtiff.so $1/imageformats/libqtiff.so
echo "Done."

echo "Copying libraries..."
cp -v ../../lmcapp/lib/liblmcapp.so.2 $1
cp -v -f ../../openssl/lib/libcrypto.so.1.0.0 $1
cp -v -f ../../openssl/lib/libssl.so.1.0.0 $1
echo "Done."

echo "Copying launch scripts..."
cp -v ./scripts/lan-messenger.sh $1/lan-messenger.sh
cp -v ./scripts/whitelist $1/whitelist
chmod 755 $1/lan-messenger.sh
chmod 755 $1/whitelist
echo "Done."

exit 0
